﻿using System;
using System.Windows.Forms;

namespace Ledict.Utility.Frm.Controls
{
    /// <summary>
    /// 分页用户控件
    /// </summary>
    [System.ComponentModel.DefaultEvent("PageNumberChanging")]
    public partial class DataPager : UserControl
    {
        public int _currentPageNumber;
        int _pageSize = Properties.Settings.Default.PageSize;
        int _pageCount = 1;
        int _totalCount;
        public DataPager()
        {
            InitializeComponent();
            lblPageSize.Text = Properties.Settings.Default.PageSize.ToString();
        }
        public event EventHandler<DataPagerPageIndexChangingEventArgs> PageNumberChanging;

        private void control_PageNumberChanged(object sender, EventArgs e)
        {
            if (object.ReferenceEquals(linklblFirst, sender))    //首页
            {
                _currentPageNumber = 1;
            }
            if (object.ReferenceEquals(linklblPrev, sender))    //上一页
            {
                _currentPageNumber -= 1;
            }
            if (object.ReferenceEquals(linklblNext, sender))    //下一页
            {
                _currentPageNumber += 1;
            }
            if (object.ReferenceEquals(linklblLast, sender))   //尾页
            {
                _currentPageNumber = _pageCount;
            }
            if (object.ReferenceEquals(GoBtn, sender))   //尾页
            {
                _currentPageNumber = Convert.ToInt32(txtCurrentPageNum.Text);
            }

            if (object.ReferenceEquals(txtCurrentPageNum, sender))   //跳转
            {
                int tempNumber;
                if (int.TryParse(txtCurrentPageNum.Text, out tempNumber))
                {
                    _currentPageNumber = tempNumber;
                }
                else
                {
                    txtCurrentPageNum.Text = _currentPageNumber.ToString();
                    return;
                }
            }
            PageInfo();
            var args = new DataPagerPageIndexChangingEventArgs((_currentPageNumber - 1) * _pageSize, _pageSize);
            OnPageNumberChanging(args);
            _totalCount = args.TotalCount;

        }

        private void PageInfo()
        {
            linklblFirst.Enabled = linklblPrev.Enabled = linklblLast.Enabled = linklblNext.Enabled = true;
            if (_totalCount == 0)
            {
                _pageCount = 1;
            }
            else
            {
                _pageCount = (((_totalCount - 1) / _pageSize) + 1);
            }

            if (_currentPageNumber <= 1)
            {
                _currentPageNumber = 1;
                linklblFirst.Enabled = linklblPrev.Enabled = false;
            }
            if (_currentPageNumber >= _pageCount)
            {
                _currentPageNumber = _pageCount;
                linklblLast.Enabled = linklblNext.Enabled = false;
            }
            lblPageCount.Text = _pageCount.ToString();
            lblCount.Text = _totalCount.ToString();
            txtCurrentPageNum.Text = _currentPageNumber.ToString();

        }

        public void ChangePageNumber(int pagenumber)
        {
            _currentPageNumber = pagenumber;
            RefreshData();
        }
        public void RefreshData()
        {
            var args = new DataPagerPageIndexChangingEventArgs((_currentPageNumber - 1) * _pageSize, _pageSize);
            OnPageNumberChanging(args);
            _totalCount = args.TotalCount;
            PageInfo();
        }
        protected virtual void OnPageNumberChanging(DataPagerPageIndexChangingEventArgs args)
        {
            if (PageNumberChanging != null)
            {
                PageNumberChanging(this, args);
            }
        }


    }

    public class DataPagerPageIndexChangingEventArgs : EventArgs
    {
        public DataPagerPageIndexChangingEventArgs(int startRowIndex, int maximumRows)
        {
            StartRowIndex = startRowIndex;
            MaximumRows = maximumRows;
        }
        public int StartRowIndex { get; private set; }
        public int MaximumRows { get; private set; }
        public int TotalCount { get; set; }
        public object DataSource { get; set; }
    }
}
